package com.zhxh.nzpos.sell;

import java.util.ArrayList;

import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.zhxh.nzpos.R;
import com.zhxh.nzpos.model.Goods;
import com.zhxh.nzpos.model.Sellout;

public class DealListAdapter extends BaseAdapter {

	private ArrayList<Sellout> items;
	private ArrayList<Goods> goodsList;
	private LayoutInflater inflater;
	private SellDeleteCallback deleteCallback;
	private Sellout item ;

	public DealListAdapter(LayoutInflater inflater,
			ArrayList<Sellout> dealItems, ArrayList<Goods> goodsList,
			SellDeleteCallback deleteCallback) {
		this.items = dealItems;
		this.goodsList = goodsList;
		this.inflater = inflater;
		this.deleteCallback = deleteCallback;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
	    item = items.get(position);
		
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.deal_list_item, null);
			holder = new ViewHolder();
			holder.identifier = (TextView) convertView
					.findViewById(R.id.goods_identifier_et);
			holder.name = (TextView) convertView
					.findViewById(R.id.goods_name_et);
			holder.guige = (TextView) convertView
					.findViewById(R.id.goods_guige_et);
			holder.sum = (EditText) convertView
					.findViewById(R.id.goods_number_et);
			holder.sum.setOnEditorActionListener(new OnEditorActionListener() {
				public boolean onEditorAction(TextView v, int actionId,
						KeyEvent event) {
					Log.i("input", "action:" + actionId);
					// if (actionId == EditorInfo.IME_ACTION_DONE) {
					Editable e = ((EditText) v).getText();
					if (e != null) {
						try {
							int num = Integer.valueOf(e.toString());
							Sellout order = items.get(position);
							order.setCount(num);
							notifyDataSetChanged();
						} catch (NumberFormatException e1) {
							e1.printStackTrace();
						}
					}
					// }
					return false;
				}
			});

			holder.unit = (TextView) convertView
					.findViewById(R.id.goods_unit_et);

			holder.unitPrice = (TextView) convertView
					.findViewById(R.id.goods_unit_price_et);
			holder.money = (TextView) convertView
					.findViewById(R.id.goods_sum_money_et);
			// holder.discount = (EditText)
			// convertView.findViewById(R.id.goods_discount_et);
			holder.deleteBtn = (Button) convertView
					.findViewById(R.id.goods_delete_btn);
			/*
			 * holder.bulkSale = (CheckBox)
			 * convertView.findViewById(R.id.bulk_sale_cb);
			 * holder.bulkSale.setOnCheckedChangeListener(new
			 * OnCheckedChangeListener() { public void
			 * onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			 * Sellout sellout = items.get(position); Goods goods =
			 * goodsList.get(position); if (isChecked) {
			 * sellout.setPrice(goods.getPrice2());
			 * sellout.setUnits(goods.getUnits2()); } else {
			 * sellout.setPrice(goods.getPrice1());
			 * sellout.setUnits(goods.getUnits2()); } notifyDataSetChanged(); }
			 * });
			 */

			holder.typeBtn = (Button) convertView
					.findViewById(R.id.goods_type_btn);
			holder.typeBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Button btn = (Button) v;
					Sellout sellout = items.get(position);

					String desp = btn.getContentDescription().toString();
					Goods goods = goodsList.get(position);
					if ("1".equals(desp)) {
						btn.setContentDescription("2");
						sellout.setPrice(goods.getPrice2());
						sellout.setUnits(goods.getUnits2());
						btn.setBackgroundResource(R.drawable.btn_sell_san);
					} else {
						btn.setContentDescription("1");
						sellout.setPrice(goods.getPrice1());
						sellout.setUnits(goods.getUnits1());
						btn.setBackgroundResource(R.drawable.btn_sell_zheng);
					}
					notifyDataSetChanged();
				}
			});

			holder.deleteBtn.setOnClickListener(new OnClickListener() {
				
			    
				@Override
				public void onClick(View v) {
					String goodsid = (String)v.getTag();
					if (deleteCallback != null) {
						deleteCallback.onDelete(goodsid);
					}
				}
			});
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		
		if (item != null) {
			holder.identifier.setText(item.getGoodId());
			holder.name.setText(item.getGoodsName());
			holder.guige.setText(item.getSpecification());
			holder.sum.setText("" + item.getCount());
			holder.unit.setText(item.getUnits());
			holder.unitPrice.setText("" + item.getPrice());
			holder.money.setText("" + item.getTotal());
			holder.deleteBtn.setTag(item.getGoodId());
			// TOTO 折扣
			// holder.discount.setText(""+item.getDiscount());
		}
		return convertView;
	}

	static class ViewHolder {
		TextView identifier;
		TextView name;
		EditText sum;
		TextView unitPrice;
		TextView unit;
		TextView money;
		TextView guige;
		CheckBox bulkSale;
		Button deleteBtn;
		Button typeBtn;
	}
}
